#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from Ui_dialog_new_attribute_type import *

class dialog_new_attribute_type(QtGui.QDialog, Ui_Dialog_new_attribute_type):
    
    
    def __init__(self, parent, ontology):
        super(dialog_new_attribute_type,self).__init__(parent)
        
        self.setupUi(self)
        self.parent=parent
        self.ontology = ontology
        
        self.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),self, QtCore.SLOT("accept()"))
        self.connect(self.buttonBox, QtCore.SIGNAL("rejected()"),self, QtCore.SLOT("reject()"))
    
    def accept(self):
        
        self.name = unicode(self.lineEdit_name.text()).encode('utf-8')
        self.type = unicode(self.lineEdit_type.text()).encode('utf-8')
        self.example = unicode(self.lineEdit_example.text()).encode('utf-8')
        self.definition = unicode(self.textEdit_definition.document().toPlainText()).encode('utf-8')
        
        if len(self.name) == 0 :
            QtGui.QMessageBox.information(self,"myCF information","You must enter a name for the new type attribute.")
            return
        for attribute_key in self.ontology.all_attributes_type:
            if self.ontology.all_attributes_type[attribute_key].name == self.name :
                QtGui.QMessageBox.warning(self,"myCF warning","A type attribute already has the name \'%s\'.\nPlease choose an other name"%(self.name))
                return
        if len(self.type) == 0 :
            QtGui.QMessageBox.information(self,"myCF information","You must enter a type for the new type attribute.")
            return
        if len(self.example) == 0 :
            QtGui.QMessageBox.information(self,"myCF information","You must enter an example for the new type attribute.")
            return
        if len(self.definition) == 0 :
            QtGui.QMessageBox.warning(self,"myCF information","You must enter a definition for the selected type attribute.")
            return
        
        QtGui.QDialog.accept(self)

   